################################### IMPORTS ###################################

from django.http import *
from django.shortcuts import render, render_to_response,redirect
from django.template import RequestContext
from django.contrib.auth import authenticate, login, logout
from django.contrib.auth.decorators import login_required
from django.contrib.auth.models import User
from django.core.exceptions import ObjectDoesNotExist

################################### VIEWS ###################################

### HOME PAGE
def home(request):
	'''-------------------------------------------------------------------------'''		
	### SETUP SEARCH BAR
	if request.POST:
		search = request.POST['search']
		url_line="/products/?csrfmiddlewaretoken=LVjD4JDIdMFwB7VYpRsenyxejVwGFzQk&sTerm="+search+"&sFilter="
		return HttpResponseRedirect(url_line)

	'''-------------------------------------------------------------------------'''		
	### DISPLAY NAME OF USER IF LOGGED IN
	# Define values needed
	user_fname=''
	user_lname=''
	login_check=False
	
	# login check
	login_check=request.user.is_authenticated()
	if login_check:
		user_fname=request.user.first_name
		user_lname=request.user.last_name

	'''-------------------------------------------------------------------------'''		
	### RENDER
	return render(request, 'accounts/home.html', {'user_fname' : user_fname, 'user_lname' : user_lname, 'login_check' : login_check})

### LOGIN PAGE
def login_user(request):
 	'''-------------------------------------------------------------------------'''		
 	### SET UP LOGIN
    # Define values needed
	state='Sign in below.'
	notice=''
	username = password = ''

    # Get input and login
	if request.POST:
		username = request.POST['username']
		password = request.POST['password']

        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                return HttpResponseRedirect('/products')                
            else:
            	notice="Account is not active. Please contact site administrators."
            	state=''
        else:
        	notice="Username and/or password is incorrect."
        	state=''

	'''-------------------------------------------------------------------------'''		
	### ASSIGN CONTEXT AND RENDER
	context={'notice' : notice, 'state' : state}                 	
	return render_to_response('accounts/login.html', context, context_instance=RequestContext(request))

### CREATE ACCOUNT PAGE
def make_new_user(request):
	'''-------------------------------------------------------------------------'''		
	### CREATE A NEW USER
	# Define values needed
	state='Fill out all fields and click submit.'
	notice=''
	fname=lname=username=email=password=re_enter_password=''
	
	# Get input and create user
	if request.POST:
		fname=request.POST['fname']
		lname=request.POST['lname']
		username=request.POST['username']
		email=request.POST['email']
		password=request.POST['password']
		re_enter_password=request.POST['re_enter_password']
		
		if fname!='' and lname!='' and username!='' and email!='' and password!='' and re_enter_password!='':			
			try:
				User.objects.get(username=username)				
				notice='There already exists a profile with the same username. Please choose another username and try again.'
				state=''
			except ObjectDoesNotExist:
				try: 
					User.objects.get(email=email)
					notice='There already exists a profile with the same email. Please choose another email address and try again.'
					state=''
				except ObjectDoesNotExist:
					if password==re_enter_password:
						user=User.objects.create_user(username=username, email=email, password=password, first_name=fname, last_name=lname)
						user.save()
						return HttpResponseRedirect('/make_new_user/congrats')
					else:
						notice='The two passwords are not equal. Please re-enter your password and try again'
						state=''
		else:
			notice='One or more of the fields has been left empty. Please fill out all of the fields and try again.'
			state=''

	'''-------------------------------------------------------------------------'''		
	### ASSIGN CONTEXT AND RENDER
	context={'state' : state, 'notice' : notice}
	return render_to_response('accounts/make_new_user.html', context, context_instance=RequestContext(request))

### SUCCESSFUL CREATED NEW ACCOUNT PAGE
def congrats(request):	
	'''-------------------------------------------------------------------------'''		
	### RENDER
	return render(request, 'accounts/congrats.html')

### LOGOUT PAGE
def logout_user(request):
	'''-------------------------------------------------------------------------'''		
	### DISPLAY NAME OF USER IF LOGGED IN
	# Define values needed
	user_fname=''
	user_lname=''
	login_check=False
	
	# login check
	login_check=request.user.is_authenticated()
	if login_check:
		user_fname=request.user.first_name
		user_lname=request.user.last_name

	'''-------------------------------------------------------------------------'''		
	### LOGOUT USER
	# Get input, logout, and redirect to home page
	if request.POST:	
		logout(request)
		return HttpResponseRedirect('/')

	'''-------------------------------------------------------------------------'''		
	### RENDER
	context={'user_fname' : user_fname, 'user_lname' : user_lname, 'login_check' : login_check}	
	return render_to_response('accounts/logout.html', context, context_instance=RequestContext(request))
